import 'package:flutter/material.dart';
import '../utils/request.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import '../provider/playAudioList.dart';
import '../utils/adapt.dart';

class RecommendPage extends StatefulWidget {
  RecommendPage({Key? key, arguments}) : super(key: key);

  @override
  _RecommendPageState createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage> {
  List _dataList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getData();
  }

  void _getData() async {
    EasyLoading.show(status: 'loading...');
    var res = await getHttp('/personalized/newsong?limit=50');
    EasyLoading.dismiss();
    var arr = res['result'];
    this.setState(() {
      _dataList = arr;
    });
  }

  @override
  Widget build(BuildContext context) {
    var playAudioList = Provider.of<PlayAudioList>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '全部新歌',
          style: TextStyle(height: 1.5, fontSize: Adapt.px(30)),
        ),
      ),
      body: ListView.separated(
        itemCount: _dataList.length,
        separatorBuilder: (BuildContext context, int index) {
          return Divider(
            height: 1,
            color: Colors.grey,
          );
        },
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            onTap: () {
              var newList = _dataList
                  .map((item) => {
                        "id": item['id'],
                        "name": item['name'],
                        "picUrl": item['picUrl']
                      })
                  .toList();
              playAudioList.setCurrentIndex(index);
              playAudioList.setAudioList(newList);
              Navigator.pushNamed(context, "/audioplayer", arguments: {});
            },
            child: ListTile(
              leading: CircleAvatar(
                backgroundImage:
                    NetworkImage(_dataList[index]['picUrl'] + '?param=60y60'),
              ),
              title: Text(
                _dataList[index]['name'],
                style: TextStyle(height: 1.5, fontSize: Adapt.px(26)),
              ),
              trailing: Icon(Icons.play_circle_filled_outlined),
            ),
          );
        },
      ),
    );
  }
}
